const express = require('express');
const router = express.Router();

//当我们使用数据库时，将它注释
// const {books} = require("../data");//引入的是文件夹，则默认引入index.js
const lodash = require("lodash");
const PagerHelper = require("../util/PagerHelper.js");
//分页插件
const pager = PagerHelper.pagerHelper;
pager.href = "/book/list.html";
console.log(pager);

const dao = require("../model/BookDao");

let searchArgs;//保存搜索的参数

router.get("/list.html", async (req, res, next) => {
    //从数据库中查询结果
    // let list;
    dao.findByPage().then(data => {
        res.render("book/list", {
            title: "书本购物车",
            username: "张三",
            books: data,
            pager, searchArgs
        })
    }).catch(err => {
        next(err);
    });
    // list = await dao.model.find();
    // console.log(list);
    // try {
        // dao.model.find().select().limit(pager.pageSize).skip(pager.pageSize * (pager.pageNum - 1))
        //     .exec((err, result) => {
        //         console.log("错误信息：", err);
        //         console.log("/book/list.html")
        //         console.log(result);
        //         return res.render("book/list", {
        //             title: "书本购物车",
        //             username: "张三",
        //             books: result,
        //             pager, searchArgs
        //         })
        //     });
        // dao.querydb().then(data => {
        //     console.log("查询成功");
        //     console.log(data);
        //     res.render("book/list", {
        //         title: "书本购物车",
        //         username: "张三",
        //         books: data,
        //         pager, searchArgs
        //     })
        // }).catch(next);
        // list = await dao.findByPage();

        // pager.setArray(list);
        // console.log(pager);
        //响应页面模板，并传递数据
        // res.json(list);
        // next();
    // } catch (e) {
    //     console.log("这是catch")
    //     console.log(e);
    //     next(e);
    // }
    // res.render("book/list", {
    //         title: "书本购物车",
    //         username: "张三",
    //         books: list,
    //         pager, searchArgs
    //     })
});

router.get("/del.do", (req, res) => {
    // lodash.remove(books, value => value.id == req.query.id);

    //重定向到list页面
    res.redirect("/book/list.html");
});
router.get("/delBatch.do", (req, res) => {
    let idsStr = req.query.ids;
    let ids = idsStr.split(",");//得到的是一个字符串数组
    lodash.remove(books, value => ids.some(id => id == value.id));
    //重定向到list页面
    res.redirect("/book/list.html");
});
router.get("/add.html", (req, res) => {
    res.render("book/add", {
        title: "添加书本"
    })
});
router.get("/edit.html", (req, res) => {
    let {id} = req.query;
    if (id) {
        let editObj = books.find(value => value.id == id);
        res.render("book/add", {
            title: "添加书本",
            editObj
        });
    } else {
        res.redirect("/book/list.html");
    }

});
router.post("/save.do", (req, res) => {
    let newObj = Object.assign({}, req.body);
    //区分添加还是编辑
    if (newObj.id) {
        //编辑
        let editObj = books.find(value => value.id == newObj.id);
        //引用数据类型的特点
        Object.assign(editObj, newObj);
    } else {
        //添加
        // books.pop();删除最后一个元素
        newObj.id = lodash.last(books).id + 1;
        //保存到数组中
        books.push(newObj);
    }

    res.redirect("/book/list.html");
});

module.exports = router;
